<template>
  <DataList 
  :excelHeader="excelHeader" 
  :cate="cate" :cols="cols"
  :name="name" 
  skipExcelNumber="3" 
 ref="datalist"
/>
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import {getXsst} from "@/api/学生社团"
import linq from "linq"
import { addZyz, deleteZyz, updateZyz, getZyz, bulkAddZyz } from "@/api/志愿者活动.js";
import {findComponent} from "@/utils/ui";
import { Promise } from 'q';
export default {
  components: {
    DataList
  },
  mounted() {
    console.log(this.$root)
  },
  data() {
    return {
      cate: "A10-5志愿者（义工社工）活动表",
      name:'10.5 志愿者活动',
      excelHeader:{B3:"学校分管部门",C3:"社团代码",D3:"社团名称(全称)",E3:"成立日期（年月）",F3:"会员数量（人）",I3:"负责人情况",
      K3:"主要活动内容",L3:"培训情况",F4:"总数",G4:"教工数",H4:"学生数",I4:"姓名",J4:"单位职务",L4:"总数（人次）",M4:"获得证书数（本）",},
      cols: [
        { label: "学校分管部门", prop: "XXFGBM" },
        { label: "社团代码", prop: "STDM" },
        { label: "社团名称(全称)", prop: "STMC" },
        { label: "成立日期(年月)", prop: "CLRQ" },
        { label: "会员数量(人)", prop: "会员数量" ,
            data:[
            {label:'总数',prop:'HY_ZS'},
            { label: "教工数", prop: "HY_JGS" },
            { label: "学生数", prop: "HY_XSS" },
        ]},
        { label: "负责人情况",
          data: [
            { label: "姓名", prop: "FZR_XM" },
            { label: "单位职务", prop: "FZR_ZW" },
        ]},
        { label: "主要活动内容", prop: "ZYHDNY" },
        { label: "培训情况", prop: "培训情况" ,
            data:[
            {label:'总数(人次)',prop:'PXQK_ZRS'},
            { label: "获得证书数(本)", prop: "PXQK_ZSS" },
        ]},
        
      ]
    };
  },
  methods: {
     getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]= "合计";
        sums[6]= "系部数:"+linq.from(list).distinct("$.XXFGBM").count();
        sums[8]= "社团数:"+linq.from(list).distinct("$.STMC").count();
        sums[10]= ""+linq.from(list).sum("$.HY_ZS");
        sums[11]= ""+linq.from(list).sum("$.HY_JGS");
        sums[12]= ""+linq.from(list).sum("$.HY_XSS");
        sums[16]= ""+linq.from(list).sum("$.PXQK_ZRS");
        sums[17]= ""+linq.from(list).sum("$.PXQK_ZSS");
        sums[18]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[18] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
        sums[18] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
        
       return sums;
    //   //
    },
    
      //导入检查方法
     check(array,list) {
     return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          var Xsst = await getXsst(user.XXID,user.CJPC)
          
          // //本身重复性检查
          // var duplicates =  linq.from(array).groupBy("$.STDM").where("$.count()>1").toArray();
          // duplicates.forEach(e=>{
          //   linq.from(array).where(t=>t.STDM==e.key()).forEach(t=>t.error="录入数据存在相同的社团代码");
          // })
          //  var err = linq.from(array).select("$.STDM+','+$.STMC").except(linq.from(Xsst).select("$.STDM+','+$.STMC").toArray()).toArray();
          // linq.from(array).where(e=>err.indexOf(e.STDM+','+e.STMC) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"不存在对应的社团代码和社团名称组合");

          //是否已经存在数据
      
          // var err =  linq.from(array).select("$.STDM").intersect(linq.from(list).select("$.STDM").toArray()).toArray();
          // linq.from(array).where(e=>err.indexOf(e.STDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在对应的社团代码");
          
          
          // //是否已经存在相同
          
          // err =  linq.from(array).select("$.STDM").except(linq.from(Xsst).select("$.STDM").toArray()).toArray();
          // linq.from(array).where(e=>err.indexOf(e.STDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"不存在对应的社团代码");
        
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },


     async  bulkAdd(array){
       await  bulkAddZyz({Zyz:array});
    },

    async fetchData() {
      var datas = await getZyz(this.$root.user.XXID,this.$root.user.CJPC);
      return datas;
    },
    
     onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
      try{     
         
          console.log(JSON.stringify(temp));
          var model = await addZyz(temp);
          console.log(model)
          resolve(model);
          }catch(e){
        reject(e);
      } 
      });
      
    },
    
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateZyz(temp);
          console.log(model)
          resolve(model);
          
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteZyz(temp);
          resolve();
      })
    }
   }
};
</script>
<style>
</style>